// pages/kc_detail/kc_detail.js
var app = getApp()
var courseid = ''
var locationId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    build_image: '',
    // location_name:"教五楼",
    // course_name:"Java",
    // teacher_username:"无无无",
    scores:"0",
    // location_x:"106.88",
    // location_y:"88.88",
    // location_describe:"无无无无无无无无",
    // describe:"无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无",
    com_list:[],
    items: [
      {name: '1', value: '1'},
      {name: '2', value: '2'},
      {name: '3', value: '3'},
      {name: '4', value: '4'},
      {name: '5', value: '5'}
      ],
      sub_scores:'',
      value: 0,
      activeNames: ['1'],
      Review:'none',
      img:'/img/Icon/down.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // 设置背景图片
    this.setData({
      build_image: app.globalData.course.imgUrl
    })
       courseid = app.globalData.course.courseId
       //请求课程
        wx.request({
          url:app.globalData.url+'v1/courses/id/'+courseid,
          method:'GET',
          success:function(res){
                console.log(res);
                  that.setData({
                    course_name:res.data.courses.name,
                    teacher_username:res.data.courses.teacherUsername,
                    describe:res.data.courses.describe,
                    locationID:res.data.courses.locationId              
                  })
                  locationId=that.data.locationID
                  // 请求地址信息
                  wx.request({
                      url:app.globalData.url+'v1/location/id/'+locationId, //必填，
                      method:'GET',
                      success:function(res){
                          console.log(res);
                          that.setData({
                                location_name:res.data.location.locationName,
                                location_x:res.data.location.locationX,
                                location_y:res.data.location.locationY,
                                //location_describe:res.data.location.locationDescribe,
                              })

                      },
                  })
          },
        })
        
    wx.request({
      url: app.globalData.url + 'v1/records/courses/'+app.globalData.course.courseId,
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {},
      success (res) {
        console.log(res)
        let list = []
        let total_scores = 0
        for(let i = 0; i < res.data.records.length; i++){
          let temp = {
            name: '',
            scores: '',
            comments: ''
          };
          /** *
          let userId = res.data.records[i].userId
          wx.request({
            url: app.globalData.url + 'v1/user/'+userId,
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res){
              //console.log(res)
              that.setData({
                name: res.data.user.name
              })
              temp.name = that.data.name
              //console.log(temp.name)
            }
          })
          */
          temp.name = res.data.users[i].name
          temp.scores = res.data.records[i].scores
          temp.comments = res.data.records[i].comments

          if(temp.scores!=0 || temp.scores!='' || temp.comments!=''){
            list.push(temp)
          }

          //计算课程总评分
          total_scores = temp.scores+total_scores
          //console.log(total_scores)
        }

        //console.log(list)
        that.setData({
          com_list:list
        })

        //动态更新课程评分（去除空评论的条数，不能直接用res.data.records.length）
        if (list.length > 0){
          console.log(list.length)
          total_scores = total_scores/list.length
          //console.log(total_scores)
          that.setData({
            scores:total_scores.toFixed(1)
          })
          //console.log(that.data.scores)
        }
      }
    })

    //根据id和courseId查一条records
    wx.request({
      //更改cid
      url: app.globalData.url + 'v1/records/'+app.globalData.userInfo.id+'/'+app.globalData.course.courseId,
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {},
      success (res) {
        //console.log(res),
        app.globalData.records = res.data.records
        //console.log(app.globalData.records.status)
      }
    })
  },
  hideReview(){
    var that =this;
    if(this.data.Review=='none'){
       that.setData({
        Review:'block',
        img:'/img/Icon/up.png'
       })
    }else{
      that.setData({
        Review:'none',
        img:'/img/Icon/down.png'
       })
    }
   
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回上一页
  backlast (){
    wx.navigateBack({
      delta: 1
    })
  },

  //跳转到签到页
  goSignPage (){
    wx.navigateTo({
      url: "/pages/signPage/signPage",
    })
  },
  //显示评论
  show_comments:function(){
    let that = this
    wx.request({
      url: app.globalData.url + 'v1/records/courses/'+app.globalData.course.courseId,
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {},
      success (res) {
        console.log(res)
        let list = []
        let total_scores = 0
        for(let i = 0; i < res.data.records.length; i++){
          let temp = {
            name: '',
            scores: '',
            comments: ''
          };
          
          temp.name = res.data.users[i].name
          temp.scores = res.data.records[i].scores
          temp.comments = res.data.records[i].comments

          if(temp.scores!=0 || temp.scores!='' || temp.comments!=''){
            list.push(temp)
          }

          //计算课程总评分
          total_scores = temp.scores+total_scores
          //console.log(total_scores)
        }

        //console.log(list)
        that.setData({
          com_list:list
        })

        //动态更新课程评分（去除空评论的条数，不能直接用res.data.records.length）
        if (list.length > 0){
          console.log(list.length)
          total_scores = total_scores/list.length
          //console.log(total_scores)
          that.setData({
            scores:total_scores.toFixed(1)
          })
          //console.log(that.data.scores)
        }
      }
    })
  },

  //获取评分
  radioChange:function(e){
    //console.log(e)
    let that = this
    that.data.sub_scores = e.detail.value
    //console.log(that.data.sub_scores)
  },
  //修改评论
  addMsg:function(e){
    let that = this
    //let scores = e.detail.value.scores
    let comments = e.detail.value.remark
    //app.globalData.records.scores = scores
    app.globalData.records.comments = comments
    //console.log(app.globalData.records)
    app.globalData.records.scores = that.data.value
    wx.request({
      method: "post",
      url: app.globalData.url + 'v1/records/update',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: app.globalData.records,
      success (res) {
        //console.log(res)
        wx.showModal({
          content:'评论成功！！',
          showCancel:false,
          confirmText:'关闭页面'
        })
        that.show_comments()
      }
    })
    that.setData({
      //sub_scores:'',
      remark:'',
      value: 0
    })
  },
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
})
